<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\memberdiscount\model;

class OrderList extends \app\BaseModel
{
    protected $name = "massage_member_discount_order_list";
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function orderResult($order_code, $transaction_id)
    {
        $order = $this->dataInfo(["order_code" => $order_code, "transaction_id" => ""]);
        if(!empty($order) && !empty($transaction_id)) {
            \think\facade\Db::startTrans();
            $user_model = new \app\massage\model\User();
            $user_info = $user_model->dataInfo(["id" => $order["user_id"]]);
            if($user_info["member_discount_time"] < time()) {
                $member_take_effect_time = time();
            } else {
                $member_take_effect_time = $user_info["member_discount_time"];
            }
            $update = ["transaction_id" => $transaction_id, "pay_type" => 2, "pay_time" => time(), "member_take_effect_time" => $member_take_effect_time];
            $res = $this->dataUpdate(["id" => $order["id"], "transaction_id" => ""], $update);
            if($res == 0) {
                \think\facade\Db::rollback();
                return false;
            }
            if(!empty($user_info) && 0 < $order["day"]) {
                $member_discount_time = $member_take_effect_time + $order["day"] * 86400;
                $res = $user_model->dataUpdate(["id" => $order["user_id"]], ["member_discount_time" => $member_discount_time, "member_discount_id" => $order["card_id"]]);
                if($res == 0) {
                    \think\facade\Db::rollback();
                    return false;
                }
                $user_info["member_discount_time"] = $member_discount_time;
                $user_info["member_discount_id"] = $order["card_id"];
            }
            $key = "longbing_user_autograph_" . $user_info["openid"];
            $key = md5($key);
            setCache($key, $user_info, 259200, 0);
            $comm_model = new \app\massage\model\Commission();
            $comm_model->dataUpdate(["order_id" => $order["id"], "type" => 24, "status" => -1], ["status" => 1]);
            \think\facade\Db::commit();
            $this->commSuccess($order["id"]);
            $this->getCoupon($order);
        }
        return true;
    }
    public function getCoupon($order)
    {
        $coupon_model = new OrderCoupon();
        $record_model = new \app\massage\model\CouponRecord();
        $data = $coupon_model->where(["order_id" => $order["id"]])->select()->toArray();
        if(!empty($data)) {
            foreach ($data as $v) {
                $record_model->recordAdd($v["coupon_id"], $order["user_id"], $v["num"]);
            }
        }
        return true;
    }
    public function commSuccess($order_id)
    {
        $comm_model = new \app\massage\model\Commission();
        $data = $comm_model->dataInfo(["order_id" => $order_id, "status" => 1, "type" => 24]);
        if(!empty($data)) {
            \think\facade\Db::startTrans();
            $res = $comm_model->dataUpdate(["id" => $data["id"], "status" => 1], ["status" => 2, "cash_time" => time()]);
            if($res == 0) {
                \think\facade\Db::rollback();
                cacheLpush("member_discount_comm", $order_id, $data["uniacid"]);
            }
            $water_model = new \app\massage\model\CoachWater();
            $res = $water_model->updateCash($data["uniacid"], $data["top_id"], $data["cash"], 1);
            if($res == 0) {
                \think\facade\Db::rollback();
                cacheLpush("member_discount_comm", $order_id, $data["uniacid"]);
            }
            \think\facade\Db::commit();
        }
        return true;
    }
    public function listCommSuccess($uniacid, $i = 10)
    {
        $num = 0;
        while ($num < $i) {
            $num++;
            $data_id = cacheLpop("member_discount_comm", $uniacid);
            if(!empty($data_id)) {
                $this->commSuccess($data_id);
            } else {
                return true;
            }
        }
        return true;
    }
}

?>